<template>
  <div class="goods-comment" v-if="commentInfo">
    <div class="head" >
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            v-for="item in commentInfo.tags"
            :key="item.title"
            :class="{ active: activeTag === item.title }"
            @click="changeTag(item.title)"
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" :class="{active:data.sortField===null}" @click="data.sortField=null">默认</a>
      <a href="javascript:;" :class="{active:data.sortField==='createTime'}" @click="data.sortField='createTime'">最新</a>
      <a href="javascript:;" :class="{active:data.sortField==='praiseCount'}" @click="data.sortField='praiseCount'">最热</a>
    </div>
    <!-- 列表 -->
    <div class="list" v-if="commentList">
      <div class="item" v-for="(item,index) in commentList" :key="index">
        <div class="user">
          <img v-src="item.member.avatar" alt="">
          <span>{{nicknameHide(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>
            <i v-for="i in 5-item.score" :key="i" class="iconfont icon-wjx02"></i>
            <span class="attr">{{specsText(item.orderInfo.specs)}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <GoodsCommentImage :pictures="item.pictures" v-if="item.pictures.length"></GoodsCommentImage>
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
    <XtxPagination @current-page="changePageFn" :total="counts" :page-size="data.pageSize" :current-page="currentPage"></XtxPagination>
  </div>
</template>

<script>
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product'
import { inject, reactive, ref, watch } from 'vue'
import { nicknameHide, specsText } from '@/hooks'
import GoodsCommentImage from './goods-comment-image.vue'

export default {
  components: { GoodsCommentImage },
  name: 'GoodsComment',
  setup () {
    //   从父级组件获取goods数据
    const goods = inject('goods')
    // 定义商品Id
    const id = goods.value.id
    // 定义空数据接收请求回来的评论头部数据
    const commentInfo = ref(null)
    /**
     * 查询商品评价信息
     * @param {String} id - 商品ID
     * @returns
     */
    findGoodsCommentInfo(id).then(({ result }) => {
      // console.log(result)
      // 将后台返回的tags数据改造成前端所需要的数据
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      //   将数据赋值传出去
      commentInfo.value = result
    })
    // 选择的评价标签，默认为全部评价
    const activeTag = ref('全部评价')

    // 评论列表数据
    const commentList = ref(null)
    /**
    * 查询商品评价列表
    * @param {String} id - 商品ID
    * @param {Object} params - 商品ID
    * @returns
    */
    //  评论总条数
    const counts = ref(0)

    // 初始化评论请求参数，值为null 的时候请求数据不会发送
    const data = reactive({
      // page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null
      // sortMethod:null
    })

    // 当前选中页码
    const currentPage = ref(1)
    const getList = () => {
      // 发送评论请求
      findGoodsCommentList(id, { ...data, page: currentPage.value }).then(({ result }) => {
        // 评论列表的赋值
        commentList.value = result.items
        // console.log(commentList, 222)
        // 评论总数的赋值
        counts.value = result.counts
      })
    }
    // 点击标签触发事件
    const changeTag = (tag) => {
      // 赋值选中的标签，获得样式
      activeTag.value = tag
      // 有图 和  全部评价  标签是后期添加上去的，需要特殊处理
      if (tag === '有图') {
        data.hasPicture = true
        data.tag = null
      } else if (tag === '全部评价') {
        data.hasPicture = null
        data.tag = null
      } else {
        data.tag = tag
        data.hasPicture = null
      }
      // 排序筛选重置为默认值
      data.sortField = null
    }

    // 侦听参数的变化，重置页码和发送请求
    watch(data, () => {
      // 初始化页码
      currentPage.value = 1
      getList()
    }, { immediate: true })

    // 当前页码
    // 页码改变事件
    const changePageFn = (page) => {
      // console.log(page)
      // 子组件点击时，当前页码数值发生改变
      currentPage.value = page
      getList()
    }
    return { commentInfo, activeTag, commentList, nicknameHide, specsText, changePageFn, currentPage, counts, data, changeTag }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
 .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
</style>
